package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminBrandService;
import com.cskaoyan.service.AdminBrandServiceimpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.TransactionUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月28日 22:41
 */
@WebServlet("/admin/brand/*")
public class AdminBrandServlet extends WdBaseServlet {

    AdminBrandService brandService = new AdminBrandServiceimpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String order = request.getParameter("order");
        String sort = request.getParameter("sort");
        //一开始没有的
        String id = request.getParameter("id");
        String name = request.getParameter("name");

        CommonData data = brandService.query(page, limit, sort, order);
        //
        System.out.println("---------------------------------测试参数-----------------------------");
        System.out.print("page = " + page);
        System.out.print(" limit = " + limit);
        System.out.print(" order = " + order);
        System.out.print(" sort = " + sort);
        System.out.print(" id = " + id);
        System.out.print(" name = " + name);
        System.out.println();
        //测试不同情况
        if (id == null && name == null) {
            System.out.println("此时关键词和用户id都是空的");
            return BaseRespVo.ok(data);
        }
        //当根据Id进行查询的时候（此时name为null或者""）
        if (id != null && ("".equals(id) == false) && (name == null || "".equals(name))) {
            System.out.println("进入根据用户id查询");
            Integer brandId = Integer.parseInt(id);
            List<MarketBrand> marketSearchHistories = brandService.selectByBrandId(brandId);
            CommonData commonData = CommonData.data(marketSearchHistories);
            return BaseRespVo.ok(commonData);
        }
        //根据品牌商名字来模糊查询的时候（此时userId为null或者""）
        if (name != null && ("".equals(name) == false) && (id == null || "".equals(id))) {
            System.out.println("进入根据关键词进行模糊查询");
            List<MarketBrand> marketSearchHistories = brandService.selectByBrandName(name);
            CommonData commonData = CommonData.data(marketSearchHistories);
            return BaseRespVo.ok(commonData);
        }
        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入brand更新");
        //格式化时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
        java.util.Date updateTime = new Date();//获得当前时间
        //处理json函数
        String jsonStr = request.getReader().readLine();
        MarketBrand marketBrand = JacksonUtil.read(jsonStr, MarketBrand.class);//获取到的就是更新以后的
        //设置更新时间
        marketBrand.setUpdateTime(updateTime);
        System.out.println(marketBrand);
        //开始更新
        brandService.update(marketBrand);
        return BaseRespVo.ok(marketBrand);
    }


    @SneakyThrows
    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入brand添加功能");
        //格式化时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
        java.util.Date updateTime = new Date();//获得当前时间
        //处理json函数
        String jsonStr = request.getReader().readLine();
        MarketBrand marketBrand = JacksonUtil.read(jsonStr, MarketBrand.class);//获取到的就是更新以后的
        //设置更新时间
        marketBrand.setUpdateTime(updateTime);
        marketBrand.setAddTime(updateTime);//添加时间和修改时间一样
        System.out.println(marketBrand);

        //开始更新
        brandService.insert(marketBrand);
        int key = marketBrand.getId();
        //处理一下id不回显的问题
        System.out.println("返回的主键是" + key);
        return BaseRespVo.ok(marketBrand);
    }

    @SneakyThrows
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入brand删除功能");
        //处理json函数
        String jsonStr = request.getReader().readLine();
        MarketBrand marketBrand = JacksonUtil.read(jsonStr, MarketBrand.class);//获取到的就是要删除的
        //测试
        System.out.println("要删除的是:");
        System.out.println(marketBrand);
        int i = brandService.delete(marketBrand.getId());
        if (i > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除错误");
        }
        return BaseRespVo.ok();
    }
}
